import { User } from '../model/user'
export class UserDao {
  public queryLoginUser(params: any): Promise<User> {
    const where = {
      account: params.account
    }
    return User.findOne({ attributes: ['id', 'account', 'email', 'name'], where: where })
  }
  public updatePassword(params: any): Promise<User> {
    return User.findOne({ where: { account: params.account } }).then(user => {
      if (user && user.id && user.password === params.oldPassword) {
        const values = {
          password: params.newPassword
        }
        return User.update(values, { where: { account: params.account } }).then(([count, users]) => {
          if (count > 0) {
            return users[0]
          } else {
            return null
          }
        })
      } else {
        return null
      }
    })
  }
  public updateUser(params: any): Promise<[number, User[]]> {
    return User.update(params, { where: { id: params.id } })
  }
}
